package com.atguigu.yygh.order.controller;


import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.common.util.JwtHelper;
import com.atguigu.yygh.enums.OrderStatusEnum;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.order.service.OrderInfoService;
import com.atguigu.yygh.vo.order.OrderCountQueryVo;
import com.atguigu.yygh.vo.order.OrderQueryVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2023-06-07
 */
@RestController
@RequestMapping("/api/order/orderInfo")
@Api("订单接口")
public class OrderInfoController {
    @Autowired
    private OrderInfoService orderInfoService;

    @PostMapping("/submitOrder/{scheduleId}/{patientId}")
    @ApiOperation(value = "创建订单")
    public R submitOder(
            @PathVariable String scheduleId,
            @PathVariable Long patientId
    ) {
        Long orderId = orderInfoService.saveOrder(scheduleId, patientId);
        return R.ok().data("orderId", orderId);
    }

    @ApiOperation(value = "订单列表，条件查询带分页")
    @GetMapping("/orderList/{pageNum}/{pageSize}")
    public R getOrderInfoPage(@PathVariable Integer pageNum,
                              @PathVariable Integer pageSize,
                              OrderQueryVo orderQueryVo,
                              @RequestHeader String token) {
        Long userId = JwtHelper.getUserId(token);
        orderQueryVo.setUserId(userId);
        Page<OrderInfo> page = orderInfoService.getOrderInfoPage(pageNum, pageSize, orderQueryVo);
        return R.ok().data("page", page);
    }

    @ApiOperation("根据订单状态查询")
    @GetMapping("/statusList")
    public R getStatusList() {
        List<Map<String, Object>> statusList = OrderStatusEnum.getStatusList();
        return R.ok().data("statusList", statusList);
    }

    @GetMapping("/detail/{orderId}")
    public R detail(@PathVariable Long orderId) {
        OrderInfo orderInfo = orderInfoService.detail(orderId);
        return R.ok().data("orderInfo", orderInfo);
    }

    @ApiOperation("取消预约")
    @GetMapping("/cancel/{orderId}")
    public R cancelOrder(@PathVariable Long orderId) {
        orderInfoService.cancelOrder(orderId);
        return R.ok();
    }

    @PostMapping("/statistics")
    public Map<String, Object> statistics(@RequestBody OrderCountQueryVo queryVo) {
        Map<String, Object> map = orderInfoService.statistics(queryVo);
        return map;
    }
}

